import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;

/*creates the graph output of the application*/
public class GraphPanel extends JPanel{

    private int maxFit,genrations;
    private int h,w;
    private ArrayList<Integer> results=null;

    public ArrayList<Integer> getResults() {
        return results;
    }

    public void setMaxFit(int x){maxFit=x;}
    public void setGenerations(int x){genrations=x;}
    public void setResults(ArrayList<Integer> a){results=a;}
    public void addResult (int x){
        if (this.results == null)
            this.results = new ArrayList<Integer>();
        this.results.add(x);
    }

    private void drawGraph(Graphics g,ArrayList<Integer> a){
	if(a!=null){
	    int ly=h-5,ny,v,nx,lx=5;
	    for(int i=0;i<a.size();i++){
		v=a.get(i).intValue();
		nx=5+(i+1)*(w-10)/genrations;
		ny=5+(maxFit-v)*(h-10)/maxFit;
		g.drawLine(lx,ly, nx, ny);
		g.drawLine(lx+1,ly+1, nx+1, ny+1);
		lx=nx;
		ly=ny;
	    }
	}

    }

    @Override
    public void paint(Graphics g){
	w=getWidth();
	h=getHeight();
	g.setColor(Color.decode("#FFFFEE"));
	g.fillRect(0, 0, w, h);
	g.setColor(Color.DARK_GRAY);
	g.drawRect(0, 0, w, h);
	g.setColor(Color.BLACK);
	g.drawLine(5, h-5, w-5, h-5);
	g.drawLine(5, 5, 5, h-5);
	for(int i=1;i<=genrations;i++){
	    g.drawLine(5+i*(w-10)/genrations, h-7, 5+i*(w-10)/genrations, h-3);
	    if(i%10==0){
	     g.setColor(Color.lightGray);
	     g.drawLine(5+i*(w-10)/genrations, 5, 5+i*(w-10)/genrations, h-7);
	     g.setColor(Color.BLACK);
	     g.drawString(i+"", 1+i*(w-10)/genrations, h-10);
	    }
	}
	int p=100;
        if (maxFit <= 10)
            maxFit = 10;
	for(int i=0;i<maxFit;i+=maxFit/10){
	    g.drawLine(3,5+i*(h-10)/maxFit, 7, 5+i*(h-10)/maxFit);
	    g.drawString(p+"%", 9, 9+i*(h-10)/maxFit);
	    p-=10;
	}
	g.setColor(Color.blue);
	drawGraph( g, this.results);
    }

}